﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HotelManager.WebUI.Models;
using HotelManager.DataAccess;
using HotelManager.DataAccess.Model;
using HotelManager.BusinessLogic.Abstract;

namespace HotelManager.WebUI.Controllers
{
    public class BookingController : Controller
    {
        private IHotelService _hotelService;

        public BookingController(IHotelService hotelService)
        {
            _hotelService = hotelService;
        }

        //
        // GET: /Booking/id

        public ActionResult Index(SearchInfoModel info, int id = 0)
        {
            //Check id from url and search model
            if (id == 0 || info == null)
            {
                return RedirectToAction("Index", "Home");
            }

            //Check time
            if (info.CheckInTime == null || info.CheckOutTime == null)
            {
                return RedirectToAction("Index", "Home");
            }

            //Getting room info
            BookingModel model = new BookingModel();
            model.RoomInfo = _hotelService.GetRoomInfo(id, info.CheckInTime.Value);

            //Parse view
            if (model.RoomInfo != null)
            {
                model.RoomInfo.From = info.CheckInTime.Value;
                model.RoomInfo.To = info.CheckOutTime.Value;
                Session["RoomInfo"] = model.RoomInfo;

                return View(model);
            }
            else
            {
                return RedirectToAction("Index", "Home");
            }
        }

        //
        // POST: /Booking/Book
        [HttpPost]
        public ActionResult Index(BookingModel model)
        {
            //Check for lost session
            if (Session["RoomInfo"] == null)
            {
                return RedirectToAction("Index", "Home");
            }

            //Get session info
            model.RoomInfo = (BookingRoomInfo)Session["RoomInfo"];

            if (ModelState.IsValid)
            {
                if (model.NumberOfRoom < 1 || model.NumberOfRoom > model.RoomInfo.AvailableQuantity)
                {
                    ModelState.AddModelError("", string.Format("Number of rooms must between {0} and {1}", 1, model.RoomInfo.AvailableQuantity));
                }
                else
                {
                    bool result = Book(model);
                    if (result)
                    {
                        return RedirectToAction("BookingCompleted", "Booking");
                    }
                    else
                    {
                        ModelState.AddModelError("", "There is an error occur!");
                    }
                }
            }
            // If we got this far, something failed, redisplay form
            return View(model);
        }

        //
        // GET: /Booking/BookingCompleted 

        public ActionResult BookingCompleted()
        {
            BookingRoomInfo model = (BookingRoomInfo)Session["RoomInfo"];
            if (model == null)
            {
                return RedirectToAction("Index", "Home");
            }

            //Clear session
            Session["RoomInfo"] = null;

            return View(model);
        }

        /// <summary>
        /// Booking
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool Book(BookingModel model)
        {
            Booking book = new Booking
            {
                DateFrom = model.RoomInfo.From,
                DateTo = model.RoomInfo.To,
                RoomTypeId = model.RoomInfo.RoomTypeId,
                NumberOfRoomBooked = model.NumberOfRoom,
                SpecialRequirement = model.SpecialRequirement,
                IsActive = true
            };

            book.Guest = new Guest
            {
                FirstName = model.FirstName,
                LastName = model.LastName,
                PhoneNumber = model.PhoneNumber,
                Address = model.Address,
                Email = model.Email,
                IsActive = true
            };

            return _hotelService.BookingHotel(book);
        }
    }
}
